/*
 * serialization.cpp
 *
 *  Created on: 19 nov. 2011
 *      Author: arthur
 */

#include "serialization.h"

void serializeImages(QList<Image> images, string fichier)
{
	FILE * fp = fopen(fichier.c_str(), "w+");

	for (int i = 0; i < images.size(); i++)
	{
		if (images[i].chemin.size() > 0)
		{
			fprintf(
					fp,
					"<image>\n\t<nom>%s</nom>\n\t<mot-clef>%s</mot-clef>\n\t<chemin>%s</chemin>\n\t<histogrames>\n",
					images[i].nom.c_str(), images[i].motClef.c_str(), images[i].chemin.c_str());
			serializeHistogrames(images[i], fp);
			fprintf(fp, "\t</histogrames>\n</image>\n");
		}
	}
	fclose(fp);
}

void serializeHistogrames(Image image, FILE * fp)
{
	if (!image.estGris)
	{
		for (int i = 0; i < 4; i++)
		{
			fprintf(fp, "\t\t<histo id=%d >\n", i);
			serializeUnHistograme(image.histogram[i], fp);
			fprintf(fp, "\t\t</histo>\n");
		}
	}
	else
	{
		fprintf(fp, "\t\t<histo id=%d >\n", Image::gris);
		serializeUnHistograme(image.histogram[Image::gris], fp);
		fprintf(fp, "\t\t</histo>\n");
	}
}

void serializeUnHistograme(float * histograme, FILE * fp)
{
	for (int i = 0; i < 256; i++)
	{
		fprintf(fp, "\t\t\t<valeur id=%d >%f </valeur>\n", i, histograme[i]);
	}
}

QList<Image> deserializeImages(string fichier)
{
	FILE * fp = fopen(fichier.c_str(), "r");
	QList<Image> images = *new QList<Image> ();
	char * tmp = (char *) malloc(200 * sizeof(char));
	fgets(tmp, 200, fp);
	while (tmp[0] == '<' && tmp[1] == 'i' && tmp[2] == 'm' && tmp[3] == 'a' && tmp[4] == 'g'
			&& tmp[5] == 'e' && tmp[6] == '>')
	{
		Image image = *new Image();
		deserializeUneImage(fp, &image);
		images.append(image);
		fgets(tmp, 200, fp);// ce doit être </image>
		fgets(tmp, 200, fp);// ce doit être <image>
	}

	fclose(fp);

	return images;
}

void deserializeUneImage(FILE * fp, Image * image)
{
	char * tmp = (char *) malloc(200 * sizeof(char));
	char * nom = (char *) malloc(100 * sizeof(char));
	char * motClef = (char *) malloc(100 * sizeof(char));
	char * chemin = (char *) malloc(200 * sizeof(char));
	fgets(tmp, 200, fp);
	deserialiseUneChaineAvecCaracteredeFin(tmp, nom, 6, 200, 100, '<');
	fgets(tmp, 200, fp);
	deserialiseUneChaineAvecCaracteredeFin(tmp, motClef, 11, 200, 100, '<');
	fgets(tmp, 200, fp);
	deserialiseUneChaineAvecCaracteredeFin(tmp, chemin, 9, 200, 200, '<');

	image->nom = nom;
	image->motClef = motClef;
	image->chemin = chemin;

	fgets(tmp, 200, fp); //doit être <histogrames>
	fgets(tmp, 200, fp); //doit être <histo id= x>
	while (tmp[0] == '\t' && tmp[1] == '\t' && tmp[2] == '<' && tmp[3] == 'h' && tmp[4] == 'i'
			&& tmp[5] == 's' && tmp[6] == 't' && tmp[7] == 'o' && tmp[8] == ' ' && tmp[9] == 'i'
			&& tmp[10] == 'd' && tmp[11] == '=' && tmp[12] >= '0' && tmp[12] <= '3' && tmp[13]
			== ' ' && tmp[14] == '>')
	{
		deserializeUnHistograme(fp, image->histogram[tmp[12] - '0']);
		fgets(tmp, 200, fp); //doit être <histo id= x> ou </histogrames>
	}

}

void deserializeUnHistograme(FILE * fp, float * histo)
{
	histo[0] = 0;
	float valeur;
	int index;
	char * tmp = (char *) malloc(200 * sizeof(char));
	fgets(tmp, 200, fp);
	while (tmp[0] == '\t' && tmp[1] == '\t' && tmp[2] == '\t' && tmp[3] == '<' && tmp[4] == 'v'
			&& tmp[5] == 'a' && tmp[6] == 'l' && tmp[7] == 'e' && tmp[8] == 'u' && tmp[9] == 'r'
			&& tmp[10] == ' ' && tmp[11] == 'i' && tmp[12] == 'd' && tmp[13] == '=')
	{
		index = 0;
		valeur = 0.0;
		int i;
		for (i = 14; tmp[i] >= '0' && tmp[i] <= '9'; i++)
		{
			index *= 10;
			index += tmp[i] - '0';
		}
		for (i = i + 2; tmp[i] >= '0' && tmp[i] <= '9'; i++)
		{
			valeur *= 10;
			valeur += tmp[i] - '0';
		}
		float facteur = 0.1;
		for (i++; tmp[i] >= '0' && tmp[i] <= '9'; i++)
		{
			valeur += (tmp[i] - '0') * facteur;
			facteur *= 0.1;
		}
		histo[index] = valeur;
		fgets(tmp, 200, fp);
	}
}
void deserialiseUneChaineAvecCaracteredeFin(char * in, char * out, int start, int maxIn,
		int maxOut, char fin)
{
	int i;
	for (i = start; i < maxIn && i - start < maxOut && in[i] != fin; i++)
	{
		out[i - start] = in[i];
	}
	for (i = i - start; i < maxOut; i++)
	{
		out[i] = '\0';
	}
}
